using System;
using System.IO;

namespace METATagsGenerator
{
	/// <summary>
	/// Summary description for Logger.
	/// </summary>
	public class Logger
	{
		//private static bool blnLogSwitch = true;
		private static bool blnLogSwitch = false;

		public Logger()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		public static bool LogSwitch
		{
			get
			{
				return blnLogSwitch;
			}
			set
			{
				blnLogSwitch = value;
			}
		}

		public static string WriteLog(string strLogMsg)
		{
			string strLogFilePath = "C:\\Temp\\UtilityLogger.txt";
			//string strLogFilePath = "d:\\logs\\UtilityLogger.txt";
			//string strLogFilePath = "d:\\Temp\\UtilityLogger.txt";
			return WriteLog(strLogMsg, strLogFilePath);
		}

		public static string WriteLog(string strLogMsg, string sLogFile)
		{
			string sRet = string.Empty;

			TextWriter TWriter = null;

			try
			{
				if (Logger.blnLogSwitch)
				{
					if (File.Exists(sLogFile))
					{
						TWriter = File.AppendText(@sLogFile);
					}
					else
					{
						string sDirectory = null;
						int iPos = sLogFile.LastIndexOf("\\");

						if (iPos<0)
						{
							// No path, only file name being passed in.
							sDirectory = "C:\\Temp";	// Use default path
						}
						else
						{
							sDirectory = sLogFile.Substring(0, iPos);
						}

						if (!Directory.Exists(sDirectory))
						{
							Directory.CreateDirectory(sDirectory);
						}
						TWriter = File.CreateText(@sLogFile);
					}
					TWriter.WriteLine(DateTime.Now + ": " + strLogMsg);
					TWriter.Close();
				}
				else
				{
					// do nothing
				}

				return sRet;
			}
			catch(Exception ex)/**/
			{
				//throw ex;
				sRet = ex.Message;
				return sRet;
			}
			finally
			{
				if (TWriter != null)
				{
					TWriter.Close();
				}
			}
		}


	}
}
